# 整理出有多少发了其他平台
import os
import re
import time

cnblog = 'cnblog'
hw = 'bbs.huawei'
juejin = 'juejin'
oschina = 'oschina'
jianshu = 'jianshu'
zhihu = 'zhihu'
toutiao = 'toutiao'
csdn = 'csdn'
cto51 = '51cto'
segmentfault = 'segmentfault'
site_cate = [hw, cnblog, juejin, oschina, csdn, cto51, zhihu, segmentfault, toutiao, jianshu]


def checks(md_file):
    path_pre = '/Users/rustfisher/Desktop/ws/wiki-ws/mk-android-wiki-proj/mk-an-wiki/docs/'
    pattern = re.compile(r': (.*?).md', re.S)
    filepath_list = []  # 这么多的md文件
    with open(md_file) as md:
        for line in md.readlines():
            line = str(line)
            cur_urls = re.findall(pattern, line)
            if len(cur_urls) > 0:
                if cur_urls[0] == '/':
                    continue
                filepath_list.append(path_pre + cur_urls[0] + '.md')

    print('md文件总数', len(filepath_list))

    pattern = re.compile(r'.*\[(.*?)\]\((.*?)\)\{(.*)\}.*', re.S)
    summary = {}
    err_filepath_list = []
    scanned_list = []
    for md_file in filepath_list:
        if not os.path.exists(md_file):
            err_filepath_list.append(md_file)
            continue
        has_lv1_title = False  # 是否有1级标题
        has_site = False
        one_title = ''
        has_publish_title = False
        with open(md_file) as md: # 开始检查一篇文章
            url_list = []
            for line in md.readlines():
                line = str(line)
                if line.startswith("# "):
                    has_lv1_title = True
                    one_title = str(line[2:]).strip()
                    continue
                if line.startswith('??? note "本文也发布在"'):
                    has_publish_title = True
                    continue
                site_info_arr = pattern.findall(line)
                got_count = len(site_info_arr)
                if has_publish_title and got_count > 0:  # 找到了目标链接
                    info = tuple(site_info_arr[0])
                    for i in info:
                        i = str(i)
                        is_site = False
                        for s in site_cate:
                            if s in i:
                                is_site = True
                                break
                        if is_site:
                            has_site = True
                            url_list.append(i)
        if has_lv1_title and has_site:
            md_dict = {'urls': url_list, 'title': one_title}
            summary[md_file] = md_dict
        # print(md_file, 'lv1 title: ', has_lv1_title)
        scanned_list.append(md_file)

    # print('路径错误的文件有:', len(err_filepath_list))
    print('published docs count: ', len(summary))
    # print(summary)

    # 开始整理结果
    output_res(summary)


def output_res(input_data):
    """
    :param input_data: {md_filepath: {urls: [], title: '文章标题'}}
    """
    res_list = []  # 准备写入文件的数据
    for mk in input_data.keys():
        one_res = {'md': mk, 'title': input_data[mk]['title']}  # 文件路径为开头
        for cate in site_cate:
            one_res[cate] = ''  # 初始化
        for url in input_data[mk]['urls']:
            url = str(url)
            for cate in site_cate:
                if cate in url:
                    one_res[cate] = url
                    continue
        res_list.append(one_res)
    t = str(time.time())
    with open('/Users/rustfisher/Desktop/ws/wiki-ws/mk-android-wiki-proj/draft/output/status' + '.csv', 'w+') as c:
        c.write('md')
        c.write(',')
        c.write('title')
        c.write(',')
        for k in site_cate:
            c.write(k)
            c.write(',')
        c.write('\n')  # 标题行写完
        for item in res_list:
            md_name = str(item['md'])[74:]
            c.write(md_name)
            c.write(',')
            c.write(item['title'])
            c.write(',')
            for k in site_cate:
                if item[k] != '':
                    c.write('Y')  # 标记
                c.write(',')
            c.write('\n')


if __name__ == '__main__':
    print('start ----- ')
    checks('/Users/rustfisher/Desktop/ws/wiki-ws/mk-android-wiki-proj/mk-an-wiki/mkdocs.yml')
